package com.fastdfs.util;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import org.junit.Test;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Paths;


/*
    <dependency>
        <groupId>com.drewnoakes</groupId>
        <artifactId>metadata-extractor</artifactId>
        <version>2.15.0</version>
    </dependency>

 */

public class ImageUtils  implements Serializable{


    private  int width; //图片宽
    private int height;  //图片高
    private  Long size; //图片类型 kb
    private  String type; //图片类型
    private  String name; //图片名称
    private static File file = null;
    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "ImageUtiles{" +
                "width=" + width +
                ", height=" + height +
                ", size=" + size +
                ", type='" + type + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    /**
     * 处理 单张 图片  
     * @param  imagepath 图片路径
     * @param  pd   是否是网络 路径
     * @return void
     * @date 2015-7-25 下午7:30:47
     */
    public void  readPic(String imagepath,boolean pd) throws Exception {
        File  jpegFile;
        if (pd){
             jpegFile= getFile(imagepath);
        }else {
             jpegFile=new File(imagepath);
        }
        Metadata metadata;
        try {
            metadata = ImageMetadataReader.readMetadata(jpegFile);
            for (Directory directory : metadata.getDirectories()) {
                for (Tag tag : directory.getTags()) {

                    if(tag.getTagName().startsWith("Image Width")){
                        width = Integer.parseInt(tag.getDescription().split(" ")[0]);
                    }
                    if(tag.getTagName().startsWith("Image Height")){
                        height=Integer.parseInt(tag.getDescription().split(" ")[0]);
                    }
                    if(tag.getTagName().startsWith("Detected File Type Name")){
                        type=tag.getDescription();
                    }
                    if(tag.getTagName().startsWith("File Size")){
                        size = Long.parseLong(tag.getDescription().split(" ")[0])/1024;
                    }
                    if(tag.getTagName().startsWith("File Name")){
                        name=tag.getDescription();
                    }

                }
            }


        } catch (ImageProcessingException | IOException e) {
            e.printStackTrace();
        }
       if (pd){
           jpegFile.delete();
       }
    }





    /**
     * 网络图片 转为File对象
     *
     * @param url 图片url
     * @return File
     * @author dyc
     * date:   2020/9/4 14:54
     */
    public static File getFile(String url) throws Exception {
        //对本地文件命名
        String fileName= url.substring(url.lastIndexOf("/")+1, url.length());
        File file = null;

        URL urlfile;
        InputStream inStream = null;
        OutputStream os = null;
        try {

            String s = System.getProperty("user.dir") + File.separator + fileName;
            file= new File(  s);
            try {
                if (!file.exists()) {
                    file.createNewFile();
                }
            }
            catch (Exception e) {
                System.out.println("新建文件操作出错");
                e.printStackTrace();
            }
            //下载
            urlfile = new URL(url);
            inStream = urlfile.openStream();
            os = new FileOutputStream(file);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
                if (null != inStream) {
                    inStream.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return file;

    }



    //链接url下载图片
    public static void downloadPicture(String url1 ,String filepath) {
        URL url = null;
        String fileName= url1.substring(url1.lastIndexOf("/")+1, url1.length());
        try {
            url = new URL(url1);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            FileOutputStream fileOutputStream = new FileOutputStream(String.valueOf(Paths.get(filepath+fileName)));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            byte[] context=output.toByteArray();
            fileOutputStream.write(context);
            dataInputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 判断图片是否存在
    public static boolean existRource(String source) {
        try {
            URL url = new URL(source);
            URLConnection uc = url.openConnection();
            InputStream in = uc.getInputStream();
            if (source.equalsIgnoreCase(uc.getURL().toString()))   //只要不异常那么就存在
                return true;
            in.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Test
    public void q(){

String[] s={"http://192.168.66.70:80/group1/M00/00/00/wKhCQ1_L-MKAXSNgAAH3JDCsYLE316.jpg",
        "http://192.168.66.70:80/group1/M00/00/00/wKhCRV_L-MKAWhPgAAIvI5FGCCI378.jpg",
"http://192.168.66.70:80/group1/M00/00/00/wKhCQ1_L-MOACtNhAAUP3Lv4Lzo243.jpg",
"http://192.168.66.70:80/group1/M00/00/00/wKhCRV_L-MOAWRavAAIfzBZoRRg593.jpg",
"http://192.168.66.70:80/group1/M00/00/00/wKhCRV_L-MaAMjqNAAIe63Rx-v4866.jpg"};
        for (String s1 : s) {
            boolean b = existRource(s1);
            System.out.println(b);
        }
    }


//网络 图片  的 详细信息
    @Test
    public void show() throws Exception {

        ImageUtils imageUtils=new ImageUtils();
        String path="https://pic.cnblogs.com/avatar/463242/20190215125753.png";
        imageUtils.readPic(path,true);
        System.out.println(imageUtils);

    }

// 本地图片测试   的 详细信息
    @Test
    public void sho1w() throws Exception {
        ImageUtils imageUtils=new ImageUtils();
        String path="D:\\1.png";
        imageUtils.readPic(path,false);
        System.out.println(imageUtils);
    }

// 下载网络图片到 本地
    @Test
    public void sh22ow() throws Exception {

        ImageUtils imageUtils=new ImageUtils();
        String path="https://pic.cnblogs.com/avatar/463242/20190215125753.png";
        imageUtils.downloadPicture(path,"D:\\");



    }

}
